import React from "react";
// import { MemoryRouter, Route } from 'react-router';
import { Link, useLocation } from "react-router-dom";
import Pagination from "@material-ui/lab/Pagination";
import PaginationItem from "@material-ui/lab/PaginationItem";

function q(query , piName , i) {
	query.set(piName , i+"")
	return query.toString()
}
export default function PaginationLink(props) {
	var location = useLocation();
	// console.log(location);
	var piName = props.piName|| "p" 
	const query = new URLSearchParams(location.search);
	const page = parseInt(query.get(piName) || "1", 10);
	// console.log("page:" , page)
	React.useEffect(() => {
		if(props.onPage) {
			props.onPage(location)
		}
	}, [location,props]);
	return (
		<Pagination
			color={props.color}
			page={page}
			count={props.count}
			renderItem={(item) => <PaginationItem component={Link} to={`${props.url}?${q(query,piName ,item.page)}`} {...item} />}
		/>
	);
}
